import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import org.rsbot.event.events.ServerMessageEvent;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.event.listeners.ServerMessageListener;
import org.rsbot.script.Script;
import org.rsbot.script.util.WindowUtil;
import org.rsbot.script.wrappers.RSArea;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSTile;

@org.rsbot.script.ScriptManifest(authors = { "Eagledude4" }, name = "EDYewWoodcutter", version = 3.57, description = "This script is a woodcutter that cuts outside east Varrock, and then banks. Also, feedback, progress reports and new ideas are extremely welcome!")

public class EDYewWoodcutter extends Script implements PaintListener, ServerMessageListener {
	
// Script Variables
public boolean objectiveSet = false;
public boolean hasHatchet = false;
public boolean canStart = false;
public boolean toldUpdate = false;

public boolean chopping = false;

public boolean isAtSouth = false;
public boolean isAtNorth = false;

public boolean goingSouth = false;
public boolean goingNorth = false;

public boolean didlast1 = false;
public boolean didlast2 = false;
public boolean didlast3 = false;
public boolean didlast4 = false;
public boolean didlast5 = false;

public boolean hour10 = false;
public boolean hour15 = false;
public boolean hour20 = false;
public boolean hour25 = false;
public boolean hour30 = false;
public boolean hour35 = false;
public boolean hour40 = false;
public boolean hour45 = false;
public boolean hour50 = false;

public boolean is99 = false;

public int bankBooth = 11402;

public int switched = 0;

public int runeHatchet = 1359;
public int dragonHatchet = 6739;
public int[] nonJunk = { 995, 1359, 6739, 1515};

public RSArea southYewArea = new RSArea(new RSTile(3266, 3466), new RSTile(3276, 3476));
public RSArea middleArea = new RSArea(new RSTile(3264, 3477), new RSTile(3272, 3485));
public RSArea northYewArea = new RSArea(new RSTile(3262, 3489), new RSTile(3272, 3499));

public RSTile bankLoc = new RSTile(3254, 3420);
public RSTile southYewLoc = new RSTile (3271, 3471);
public RSTile middleLoc = new RSTile(3268, 3481);
public RSTile northYewLoc = new RSTile (3267, 3494);

public RSTile[] bankToYews;
public RSTile[] yewsToBank;


//Paint Variables (Credits to Epic)
public boolean hasBanked = false;
public boolean showPaint = false;

public double version = 3.57;
public double latestVersion = 0;

public int timesBanked = 0;

public int totalLogsP = 0;
public int yewPrice = 0;
public int profit = 0;
	
public long startTime = 0;
public long EXPGained;

public int startLevel;
public int startEXP;
public int levelsGained;

public String status = "";


//Paint Methods
	public void onRepaint(Graphics g) {
		if (game.isLoggedIn()) {
			drawMouse(g);
			long millis = System.currentTimeMillis() - startTime;
			long hours = millis / (1000 * 60 * 60);
			millis -= hours * (1000 * 60 * 60);
			long minutes = millis / (1000 * 60);
			millis -= minutes * (1000 * 60);
			long seconds = millis / 1000;
			EXPGained = skills.getCurrentExp(8) - startEXP;
			levelsGained = skills.getCurrentLevel(8) - startLevel;
			int x;
			int LTL;
			LTL = skills.getExpToNextLevel(8) / 175;
			
			g.setColor(new Color(0, 0, 0, 150));
			g.fillRect(197, 1, 321, 120);
			x = 200;
			g.setColor(Color.GREEN);
			g.drawString(""+status, x, 40);
			g.setColor(Color.WHITE);
			if (status == "Checking") {
				g.drawString("Runtime: 0:0:0", x, 55);
			} else if (status != "Checking") {
				g.drawString("Runtime: "+hours+":"+minutes+":"+seconds, x, 55);
			}
			g.drawString("Version: "+version, x, 70);
			if (version < latestVersion) {
				g.setColor(Color.RED);
				g.drawString("***Outdated***", x, 85);
			} else {
				g.setColor(Color.GREEN);
				g.drawString("Latest Version", x, 85);
			}
			
			x = 300;
			g.setColor(Color.WHITE);
			g.drawString("Current Level: "+skills.getCurrentLevel(8), x, 40);
			g.drawString("Levels Gained: "+levelsGained, x, 55);
			if (EXPGained < 1000) {
				g.drawString("EXP Gained: "+EXPGained+"xp", x, 70);
			} else if (EXPGained > 1000  && EXPGained < 10000) {
				g.drawString("EXP Gained: "+EXPGained / 1000+"k xp", x, 70);
			} else if (EXPGained > 10000 && EXPGained < 100000) {
				g.drawString("EXP Gained: "+EXPGained/ 1000+"k xp", x, 70);
			} else if (EXPGained > 100000 && EXPGained < 1000000) {
				g.drawString("EXP Gained: "+EXPGained / 1000+"k xp", x, 70);
			} else if (EXPGained > 1000000) {
				g.drawString("EXP Gained: "+EXPGained / 1000000+"m xp", x, 70);
			}
			if (LTL < 1000) {
				g.drawString("Logs till level: "+LTL+"", x, 85);
			} else if (LTL > 1000  && LTL< 10000) {
				g.drawString("Logs till level: "+LTL / 1000+"k", x, 85);
			} else if (LTL > 10000 && LTL < 100000) {
				g.drawString("Logs till level: "+LTL / 1000+"k", x, 85);
			}	
			
			x = 415;
			if (timesBanked == 0) {
				g.drawString("Banked: Not Yet", x, 40);
				g.drawString("Logs In Bank: TBA", x, 55);
			} else if (timesBanked == 1) {
				g.drawString("Banked: Once", x, 40);
			} else if (timesBanked > 1) {
				g.drawString("Banked: "+timesBanked+" times", x, 40);
			}
			if (totalLogsP < 1000 && timesBanked != 0) {
				g.drawString("Logs In Bank: "+totalLogsP+"", x, 55);
			} else if (totalLogsP > 1000 && totalLogsP < 10000) {
				g.drawString("Logs In Bank: "+totalLogsP / 1000+"k", x, 55);;
			} else if (totalLogsP > 10000 && totalLogsP < 100000) {
				g.drawString("Logs In Bank: "+totalLogsP / 1000+"k", x, 55);
			} else if (totalLogsP > 100000 && totalLogsP < 1000000) {
				g.drawString("Logs In Bank: "+totalLogsP / 1000+"k", x, 55);
			} else if (totalLogsP > 1000000) {
				g.drawString("Logs In Bank: "+totalLogsP / 1000000+"m", x, 55);
			}
			g.drawString("Log Price: "+yewPrice+" gp", x, 70);
			g.setColor(Color.GREEN);
			if (profit < 1000) {
				g.drawString("Profit: "+profit+" gp", x, 85);
			} else if (profit > 1000  && profit < 10000) {
				g.drawString("Profit: "+profit / 1000+"k", x, 85);
			} else if (profit > 10000 && profit < 100000) {
				g.drawString("Profit: "+profit / 1000+"k", x, 85);
			} else if (profit > 100000 && profit < 1000000) {
				g.drawString("Profit: "+profit / 1000+"k", x, 85);;
			} else if (profit > 1000000) {
			g.drawString("Profit: "+profit / 1000000+"m", x, 85);
			}
			
			g.setColor(Color.WHITE);
			g.drawRect(202, 94, 311, 21);
			g.setColor(Color.RED);
			g.fillRect(203, 95, 310, 20);
			g.setColor(Color.GREEN);
			g.fillRect(203, 95, (310*skills.getPercentToNextLevel(8))/100, 20);
			g.setColor(Color.WHITE);
			g.drawString(+skills.getPercentToNextLevel(8)+"%", 357, 109);
			
			g.setColor(Color.WHITE);
			g.drawString("ED Yew Woodcutter", 305, 20);
		}
	}
	
	private void drawMouse(final Graphics g) {
	final Point loc = mouse.getLocation();
		if (loc.x < 516) {
			g.setColor(Color.GREEN);
		} else if (loc.x > 516)
			g.setColor(Color.RED);
			g.drawOval(loc.x - 14, loc.y - 14, 28, 28);
			g.fillOval(loc.x - 7, loc.y - 7, 14, 14);
	}
	
	public void getLatestVersion() {
	URLConnection url = null;
	BufferedReader in = null;
		try {
			url = new URL("http://karmagames.no-ip.biz/EDYewWoodcutter.txt").openConnection();
			in = new BufferedReader(new InputStreamReader(url.getInputStream()));
			latestVersion = Double.parseDouble(in.readLine());
		} catch (final Exception e) {
		}
	}

	
//Script Methods  
	public boolean onStart() {
		if (game.isLoggedIn()) {
			if (skills.getCurrentLevel(8) == 99) {
				is99 = true;
			}
		}
		return true;
	}
	
	public void onFinish() {
		log("Thanks for using my script, check forum for updates.");
		return;
	}
	
	public int loop() {
	RSTile[] bankToYews = walking.findPath(southYewLoc);
	RSTile[] yewsToBank = walking.findPath(bankLoc);
	long millis = System.currentTimeMillis() - startTime;
	long hours = millis / (1000 * 60 * 60);
	millis -= hours * (1000 * 60 * 60);
	long minutes = millis / (1000 * 60);
	millis -= minutes * (1000 * 60);
	long seconds = millis / 1000;
	try {
		if (game.isLoggedIn()) {
			profit = (timesBanked * 28 * yewPrice) + (inventory.getCount(1515) * yewPrice);
			if (hasHatchet == false) {
				game.openTab(4);
				checkInventory();
			}
			
			if (hasHatchet == true) {				
				camera.setPitch(true);
			
			if (latestVersion > version && toldUpdate == false) {
				WindowUtil.showDialog("This version of EDYewWoodcutter is now outdated!");
				log("This version of EDYewWoodcutter is now outdated!");
				log("Get the newest version at http://bit.ly/EDYewWc");
				toldUpdate = true;
			}
			
			if (yewPrice == 0) {
				yewPrice = grandExchange.loadItemInfo(1515).getMarketPrice();
			}
			
			if (hours == 10 && minutes == 0 && seconds == 0) {
				hour10 = true;
				log("Nice, 10 hours! Screenshot :)");
			} else {
				hour10 = false;
			}
			
			if (hours == 15 && minutes == 0 && seconds == 0) {
				hour15 = true;
				log("Nice, 15 hours! Screenshot :)");
			} else {
				hour15 = false;
			}
			
			if (hours == 20 && minutes == 0 && seconds == 0) {
				hour20 = true;
				log("Nice, 20 hours! Screenshot :)");
			} else {
				hour20 = false;
			}
			
			if (hours == 25 && minutes == 0 && seconds == 0) {
				hour25 = true;
				log("Nice, 25 hours! Screenshot :)");
			} else {
				hour25 = false;
			}
			
			if (hours == 30 && minutes == 0 && seconds == 0) {
				hour30 = true;
				log("Nice, 30 hours! Screenshot :)");
			} else {
				hour30 = false;
			}
			
			if (hours == 35 && minutes == 0 && seconds == 0) {
				hour35 = true;
				log("Nice, 35 hours! Screenshot :)");
			} else {
				hour35 = false;
			}
			
			if (hours == 40 && minutes == 0 && seconds == 0) {
				hour40 = true;
				log("Nice, 40 hours! Screenshot :)");
			} else {
				hour40 = false;
			}
			
			if (hours == 45 && minutes == 0 && seconds == 0) {
				hour45 = true;
				log("Nice, 45 hours! Screenshot :)");
			} else {
				hour45 = false;
			}
			
			if (hours == 50 && minutes == 0 && seconds == 0) {
				hour50 = true;
				log("Nice, 50 hours! Screenshot :)");
			} else {
				hour50 = false;
			}
		
			if (hour10 == true || hour15 == true || hour20 == true || hour25 == true || hour30 == true || hour35 == true || hour40 == true || hour45 == true || hour50 == true) {
				env.saveScreenshot(true);
			}
			
			if (is99 == false && skills.getCurrentLevel(8) == 99) {
				env.saveScreenshot(true);
				log("Grats on 99, Wo0T!");
				is99 = true;
			}
			
			if (startTime == 0 && skills.getCurrentLevel(8) != 0) {
				startTime = System.currentTimeMillis();
				startLevel = skills.getCurrentLevel(8);
				startEXP = skills.getCurrentExp(8);
			}
			
			if (walking.getEnergy() == 100 && getMyPlayer().isMoving()) {
				walking.setRun(true);
			}
			
			if (inventory.isFull()) {
				inventory.dropAllExcept(nonJunk);
				if (calc.distanceTo(bankLoc) < 5) {
					useBank();
				} else if (getMyPlayer().getLocation().getY() < 3499) {
					status = "Walking";
					walking.walkPathMM(yewsToBank);
				}
			}
			if (!inventory.isFull() && getMyPlayer().getLocation().getY() < 3466) {
				status = "Walking";
				walking.walkPathMM(bankToYews);
				walking.walkTileMM(southYewLoc);
			}
			
			if (!inventory.isFull() && isAtSouth == true) {
				chopSouthTree();
				goingSouth = false;
			} else if (!inventory.isFull() && isAtNorth == true) {
				chopNorthTree();
				goingNorth = false;
			}
			
			if (middleArea.contains(getMyPlayer().getLocation())) {
				if (goingNorth == true) {
					walking.walkTileMM(northYewLoc);
				} else if (goingSouth == true) {
					walking.walkTileMM(southYewLoc);
				}
			}
				
			if (southYewArea.contains(getMyPlayer().getLocation())) {
				isAtSouth = true;
			} else if (!southYewArea.contains(getMyPlayer().getLocation())) {
				isAtSouth = false;
			}
			if (northYewArea.contains(getMyPlayer().getLocation())) {
				isAtNorth = true;
			} else if (!northYewArea.contains(getMyPlayer().getLocation())) {
				isAtNorth = false;
			}
			}
		}
		} catch (final Exception e) {
		}
	return 500;
	}

	
	public void logout() {
		mouse.move(753, 11);
		mouse.click(true);
		mouse.move(652, 421);
		mouse.click(true);
	}
		
	public void checkInventory() {
    RSItem runeHatchet = inventory.getItem(1359);
    RSItem dragonHatchet = inventory.getItem(6739);
    	game.openTab(4);
    	sleep(1000);
    	if (inventory.contains(1359) && skills.getCurrentLevel(0) >= 41) {
    		runeHatchet.doClick(true);
    		hasHatchet = true;
    	} else if (inventory.contains(6739) && skills.getCurrentLevel(0) >= 61) {
    		dragonHatchet.doClick(true);
    		hasHatchet = true;
    	}
    	if (inventory.contains(1359) && skills.getCurrentLevel(0) < 41) {
    		hasHatchet = true;
    	} else if (inventory.contains(6739) && skills.getCurrentLevel(0) < 61) {
    		hasHatchet = true;
    	} 
    	if (!inventory.contains(1359) && !inventory.contains(6739)) {
    		checkEquipment();
    	}
    }
    
    public void checkEquipment() {
		game.openTab(5);
		sleep(1000);
		if (equipment.containsOneOf(runeHatchet) == true || equipment.containsOneOf(dragonHatchet) == true) {
			hasHatchet= true;
		} else if (equipment.containsOneOf(runeHatchet) == false && equipment.containsOneOf(dragonHatchet) == false) {
			WindowUtil.showDialog("You don't have an appropriate hatchet");
			log("You must have either a rune or dragon hatchet equiped or in your");
			log("inventory to use this script.");
			logout();
			sleep(1000);
			stopScript();
		}
    }
        	
    
    public void chopSouthTree() {
    RSObject Tree = objects.getNearest(1309);
    RSTile treeLoc = Tree.getLocation();
    	if (getMyPlayer().getAnimation() == 867 && calc.distanceTo(treeLoc) <= 3) {
        	chopping = true;
        } else {
        	chopping = false;
        }
        	
    	if (calc.distanceTo(treeLoc) > 5 && calc.distanceTo(southYewLoc) <= 3) {
    		if (switched != 2) {
    			status = "No Tree";
    			walking.walkTileMM(middleLoc);
    			switched++;
    			goingNorth = true;
    		} else if (switched == 2) {
        		status = "No Tree";
        		antiBan();
        	}
        }
        	
        if (chopping == false && calc.distanceTo(treeLoc) <= 5) {
        	if (!getMyPlayer().isMoving()) {
        		Tree.doAction("Chop down");
        		status = "Cutting Yew";
        		switched = 0;
        		antiBan();
        	}
        }
    }
       	
    public void chopNorthTree() {
    RSObject Tree = objects.getNearest(1309);
    RSTile treeLoc = Tree.getLocation();
    	if (getMyPlayer().getAnimation() == 867 && calc.distanceTo(treeLoc) <= 3) {
    		chopping = true;
        } else {
        	chopping = false;
        }
        	
    	if (calc.distanceTo(treeLoc) > 5 && calc.distanceTo(northYewLoc) <= 3) {
        	if (switched != 2) {
        		status = "No Tree";
        		walking.walkTileMM(middleLoc);
        		switched++;
        		goingSouth = true;
        	} else if (switched == 2) {
        		status = "No Tree";
        		antiBan();
        	}
        }
        	
        if (chopping == false && calc.distanceTo(treeLoc) <= 5) {
        	if (!getMyPlayer().isMoving()) {
        		Tree.doAction("Chop down");
        		status = "Cutting Yew";
        		switched = 0;
        		antiBan();
        	}
        }
    }
   	
    public void useBank() {
	final RSObject bankbooth = objects.getNearest(bankBooth);
	try {
		if (bank.getInterface().isValid()) {
			depositLogs();
		} else if (!getMyPlayer().isMoving() && !bank.getInterface().isValid()) {
			bankbooth.doAction("use-quickly");
		}
		} catch (final Exception e) {
		}
	}
		
	public void depositLogs() {
	final RSObject bankbooth = objects.getNearest(bankBooth);
	RSItem Log = inventory.getItem(1515);
	int Bank = bank.getCount(1515);
	int Inventory = inventory.getCount(1515);
	int totalLogs = Bank + Inventory;	
	try {
		if (!bank.getInterface().isValid() && !getMyPlayer().isMoving() && bankbooth != null) {
			menu.doAction("use-quickly");
		} else if (bank.getInterface().isValid()) {
			status = "Banking";
			if (inventory.contains(runeHatchet) || inventory.contains(dragonHatchet)) {
				Log.doAction("Deposit-All");
				sleep(random(4000, 4500));
				timesBanked++;
				totalLogsP = totalLogs;
				mouse.move(random(483, 494), random(28, 39));
				sleep(random(800, 1200));
				mouse.click(true);
			} else if (!inventory.contains(runeHatchet) && !inventory.contains(dragonHatchet)) {
				sleep(random(1000, 1500));
				mouse.move(random(382, 410), (random(299, 312)));
				sleep(random(800, 1200));
				mouse.click(true);
				sleep(random(4000, 4500));
				timesBanked++;
				totalLogsP = totalLogs;
				mouse.move(random(483, 494), random(28, 39));
				sleep(random(800, 1200));
				mouse.click(true);
			}
		}
		} catch (final Exception e) {
		}
	}
			
	public void serverMessageRecieved(ServerMessageEvent arg0) {
	String serverString = arg0.getMessage();
		try {
			if (serverString.contains("<col=ffff00>System update in")) {
				WindowUtil.showDialog("System update, shutting down");
				log("System update, shutting down.");
				env.saveScreenshot(true);
				logout();
				sleep(1000);
				stopScript();
			} else if (serverString.contains("You've just advanced")) {
		        log("Level Up! You're welcome :)");
		        env.saveScreenshot(true);
		    }
			} catch (final Exception e) {
		}
	}
			
	public void antiBan() {
	final int ranNo = random(1, 51);
		try {
			if (ranNo == 1 && didlast1 == false) {
				game.openTab(1);
				mouse.move(random(682, 726), random(354, 370));
				sleep(random(2000, 3000));
				game.openTab(4);
				didlast1 = true;
				didlast2 = false;
				didlast3 = false;
				didlast4 = false;
				didlast5 = false;
			} else if (ranNo == 2 && didlast2 == false) {
				game.openTab(9);
				sleep(random(800, 1200));
				mouse.click(true);
				sleep(random(2000, 3000));
				game.openTab(4);
				didlast1 = false;
				didlast2 = true;
				didlast3 = false;
				didlast4 = false;
				didlast5 = false;
			} else if (ranNo == 3 && didlast3 == false) {
				camera.setAngle(random(90, 180));
				sleep(random(400, 800));
				didlast1 = false;
				didlast2 = false;
				didlast3 = true;
				didlast4 = false;
				didlast5 = false;
			} else if (ranNo == 4 && didlast4 == false) {
				mouse.move(random(25, 500), random(16, 291));
				sleep(random(800, 1200));
				didlast1 = false;
				didlast2 = false;
				didlast3 = false;
				didlast4 = true;
				didlast5 = false;
			} else if (ranNo == 5 && didlast5 == false) {
				mouse.moveOffScreen();
				sleep(random(10000, 25000));
				didlast1 = false;
				didlast2 = false;
				didlast3 = false;
				didlast4 = false;
				didlast5 = true;
			}
			} catch (final Exception e) {
		}
	}
}